package com.googlecode.jlsf.swing;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JSplitPane;
import javax.swing.UIManager;

/**
 * Main frame of application
 */
public class MainFrame extends JFrame
{
	private static final long serialVersionUID = 8244638710307374166L;
	
	protected boolean changed = false;
	
	public static void main(String[] args) 
	{
		try
		{
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		new MainFrame().setVisible(true);
	}
	
	private MainFrame()
	{
		super(ResourceManager.getString("title"));     
	
		this.setIconImage(ResourceManager.getImage("app.png"));
	
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		this.setJMenuBar(createMenu());
		
		this.getContentPane().add(new JSplitPane(
				JSplitPane.HORIZONTAL_SPLIT,
				new LeftPanel(),
				new RightPanel()));
				
		this.pack();
	}
	
	private JMenuBar createMenu()
	{
		JMenuBar menu = new JMenuBar();
		
		JMenu file = new JMenu(ResourceManager.getString("menu.file"));
		menu.add(file);
		file.add(getLoadAction());
		file.add(getSaveAction());
		file.addSeparator();
		file.add(getOptionsAction());
		file.addSeparator();
		file.add(getExitAction());
		
		JMenu help = new JMenu(ResourceManager.getString("menu.help"));
		menu.add(help);
		help.add(getAboutAction());
		
		return menu;
	}

	private Action getLoadAction()
	{
		AbstractAction a = new AbstractAction(ResourceManager.getString("action.load"),
				ResourceManager.getIcon("load.png")) 
		{
			private static final long serialVersionUID = -5624688501300022840L;

			public void actionPerformed(ActionEvent e)
			{
				
			}
			
		};
		
		a.putValue(Action.SHORT_DESCRIPTION, ResourceManager.getString("action.load.description"));
		
		return a;
	}
	
	private Action getSaveAction()
	{
		AbstractAction a = new AbstractAction(ResourceManager.getString("action.save"),
				ResourceManager.getIcon("save.png")) 
		{
			private static final long serialVersionUID = -8324687369196949921L;

			public void actionPerformed(ActionEvent e)
			{
				save();
			}
			
		};
		
		a.putValue(Action.SHORT_DESCRIPTION, ResourceManager.getString("action.save.description"));
		
		return a;
	}
	
	private Action getOptionsAction()
	{
		AbstractAction a = new AbstractAction(ResourceManager.getString("action.options"),
				ResourceManager.getIcon("options.png")) 
		{
			private static final long serialVersionUID = 3360360364812454866L;

			public void actionPerformed(ActionEvent e)
			{
				
			}
			
		};
		
		a.putValue(Action.SHORT_DESCRIPTION, ResourceManager.getString("action.options.description"));
		
		return a;
	}
	
	
	private Action getExitAction()
	{
		AbstractAction a = new AbstractAction(ResourceManager.getString("action.exit"),
				ResourceManager.getIcon("exit.png")) 
		{
			private static final long serialVersionUID = -5624688501300022840L;

			public void actionPerformed(ActionEvent e)
			{
				if(saveBefore())
				{
					System.exit(0);
				}
			}
			
		};
		
		a.putValue(Action.SHORT_DESCRIPTION, ResourceManager.getString("action.exit.description"));
		
		return a;
	}
	
	private Action getAboutAction()
	{
		AbstractAction a = new AbstractAction(ResourceManager.getString("action.about"),
				ResourceManager.getIcon("info.png")) 
		{
			private static final long serialVersionUID = -8324687369196949921L;

			public void actionPerformed(ActionEvent e)
			{
				new AboutDialog().setVisible(true);
			}
			
		};
		
		a.putValue(Action.SHORT_DESCRIPTION, ResourceManager.getString("action.about.description"));
		
		return a;
	}
	
	private boolean saveBefore()
	{
		if(changed)
		{
			int answer = JOptionPane.showConfirmDialog(this, 
					ResourceManager.getString("changed.confirm"), 
					ResourceManager.getString("action.save"), 
					JOptionPane.YES_NO_CANCEL_OPTION);
			
			if(answer == JOptionPane.YES_OPTION && save() || answer == JOptionPane.NO_OPTION)
			{
				changed = false;
				return true;
			}
			else
			{
				return false;
			}
			
		}
		
		else
		{
			return true;
		}
		
	}
	
	private boolean save()
	{
		JFileChooser fc = new JFileChooser();

		if(fc.showSaveDialog(MainFrame.this) == JFileChooser.APPROVE_OPTION)
		{
			try
			{
				//XMLHelper.saveInto(this, fc.getSelectedFile());
				return true;
			}
			catch(Exception e)
			{
				//info.showViewPanel(e);
			}
		}
		
		return false;
	}
	

}
